{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "a2766832",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ENCODING_UTT_8 = 'utf-8'\n",
    "ENCODING_UTT_16 = 'utf-16'\n",
    "ENCODING_UTT_32 = 'utf-32' \n",
    "\n",
    "\"\"\"\n",
    "ASCII编码每个字母或符号占1byte（8bits），\n",
    "    并且8bits的最高位是0，因此ASCII能编码的字母和符号只有128个。\n",
    "    有一些编码把8bits最高位为1的后128个值也编码上，使得1byte可以表示256个值，\n",
    "    但是这属于扩展的ASCII，并非标准ASCII。通常所说的标准ASCII只有前128个值！\n",
    "\"\"\"\n",
    "ENCODING_ASCII = 'ascii'\n",
    "\n",
    "\"\"\" GB2312 \n",
    "    在GB2312中收录了6763个汉字以及682个特殊符号，已经囊括了生活中最常用的所有汉字\n",
    "\"\"\"\n",
    "ENCODING_GB2312 = 'gb2312'\n",
    "\n",
    "\"\"\" GBK编码,\n",
    "    可以表示的汉字达到了20902个，另有984个汉语标点符号、部首等。\n",
    "    值得注意的是这20902个汉字还包含了繁体字，但是该繁体字与台湾Big5编码不兼容，\n",
    "    因为同一个繁体字很可能在GBK和Big5中数字编码是不一样的。\n",
    "\"\"\"\n",
    "ENCODING_GBK = 'gbk'\n",
    "\n",
    "\"\"\"GB18030\n",
    "    因此GB18030多出来的汉字使用4bytes编码。\n",
    "    当然，为了兼容GBK，这个四字节的前两位显然不能与GBK冲突（实操中发现后两位也并没有和GBK冲突）\n",
    "\"\"\"\n",
    "ENCODING_GB18030 = 'gb18030'\n",
    "\n",
    "\n",
    "\"\"\" 文件读写模式\n",
    "r  : 只进行读的操作，需要文件存在，进行读操作时，文件的光标在开头的第一个字节。\n",
    "r+ : 即可读也可以写，需要文件存在。\n",
    "\n",
    "w  : 只对文件进行全覆盖写入，若文件不存在，则会新建一个文件，文件光标处于文件开头。\n",
    "w+ : 可读可写（全覆盖），若文件不存在，则会新建一个文件，文件光标处于文件开头。\n",
    "\n",
    "a  : 对文件进行追加，若文件不存在会新建文件，文件光标位于文件末尾。\n",
    "a+ : 可读可写，若文件不存在则会新建文件，文件光标处于文件末尾。\n",
    "\n",
    "rb : 读取文件二进制\n",
    "wb : 写入文件二进制\n",
    "\"\"\"\n",
    "1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "9bcf0dd3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def read_file(path, mode='r', encoding='utf-8', sep=None):\n",
    "    \"\"\" 读取文件 \"\"\"\n",
    "    if mode not in ['r', 'r+', 'rb', 'a+', 'w+']:\n",
    "        raise Exception(f\"请检查文件读取模式:{mode} 模式可选 'r', 'r+', 'rb', 'a+'\")\n",
    "    if mode in ['rb']:\n",
    "        encoding = None\n",
    "    with open(path, mode=mode, encoding=encoding) as f:\n",
    "        data = f.read()\n",
    "    if sep and isinstance(data, str):\n",
    "        data = data.split(sep)\n",
    "    return data\n",
    "\n",
    "\n",
    "def write_file(obj, path, mode='w', encoding='utf-8'):\n",
    "    \"\"\" 写入文件 \"\"\" \n",
    "    if mode not in ['w', 'w+', 'wb', 'a', 'a+', 'r+']:\n",
    "        raise Exception(f\"请检查文件读取模式:{mode} 模式可选 'w', 'w+', 'wb', 'a', 'a+', 'r+' \")\n",
    "    if mode in ['wb']:\n",
    "        encoding = None\n",
    "    with open(path, mode=mode, encoding=encoding) as f:\n",
    "        f.write(obj)\n",
    "        \n",
    "\n",
    "# p_path = \"data/102-文本2.txt\"\n",
    "# # d = read_file(path=p_path, mode='rb', encoding='utf-8', sep=None)\n",
    "\n",
    "# texts = \"你说什么事时候\\n\"\n",
    "# write_file(obj=texts, path=p_path, mode='a', encoding='utf-8')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "31b4d98c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1: 1, 3: 2}"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pickle\n",
    "\n",
    "def pack(obj, path):\n",
    "    with open(path, 'wb') as f:\n",
    "        pickle.dump(obj, f)\n",
    "        \n",
    "def unpack(path):\n",
    "    with open(path, 'rb') as f:\n",
    "        obj = pickle.load(f)\n",
    "    return obj\n",
    "\n",
    "obj = {1:1, 3:2}\n",
    "f_path = \"data/d.pkl\"\n",
    "\n",
    "pack(obj, path=f_path)\n",
    "unpack(f_path)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "af9714a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "pickle.dump??"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "f671ed1e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'\\xe4\\xbd\\xa0\\xe8\\xaf\\xb4\\xe4\\xbb\\x80\\xe4\\xb9\\x88\\xe4\\xba\\x8b\\xe6\\x97\\xb6\\xe5\\x80\\x99'"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = read_file(path=p_path, mode='rb', encoding='utf-8', sep=None)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "bb500a37",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你说什么事时候'"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(b, encoding=ENCODING_UTT_8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "39b43a51",
   "metadata": {},
   "outputs": [],
   "source": [
    "str??"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "3e3eb9ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p_path = \"data/102-文本2.txt\"\n",
    "\n",
    "texts = \"你们 其实都不错\"\n",
    "\n",
    "with open(p_path, 'r', encoding=ENCODING_UTT_32) as f:\n",
    "    ss_ = f.read()\n",
    "    \n",
    "isinstance(ss_, str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16249ebf",
   "metadata": {},
   "outputs": [],
   "source": [
    "(texts,)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "58b04572",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "utf-8\n"
     ]
    }
   ],
   "source": [
    "import chardet\n",
    "\n",
    "# f_path = \"data/102-文本.txt\"\n",
    "\n",
    "with open(f_path, 'rb') as reader:\n",
    "#     line = reader.readline()\n",
    "    \n",
    "    line = reader.read()\n",
    "print(chardet.detect(line)['encoding'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f7875791",
   "metadata": {},
   "outputs": [],
   "source": [
    "open??"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3c5262a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a8c6e548",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07601c35",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37624900",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    " \n",
    " \n",
    "# 获取文件编码类型\n",
    "def get_encoding(file):\n",
    "    # 二进制方式读取，获取字节数据，检测类型\n",
    "    with open(file, 'rb') as f:\n",
    "        data = f.read()\n",
    "        return chardet.detect(data)['encoding']\n",
    " \n",
    " \n",
    "file_name = 'AAAA.txt'\n",
    "encoding = get_encoding(file_name)\n",
    "print(encoding)\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
